package com.javaclimb.drug.controller;

import com.javaclimb.drug.common.LogUtil;
import com.javaclimb.drug.common.ResultMapUtil;
import com.javaclimb.drug.entity.ILogInfo;
import com.javaclimb.drug.entity.User;
import com.javaclimb.drug.service.IUserService;
import com.javaclimb.drug.service.LogService;
import com.javaclimb.drug.service.impl.UserServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.security.Security;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 用户相关的controller
 */
@Controller
public class UserController {

    @Autowired
    IUserService userService;
    @Autowired
    private LogService logService;

    /**
     * 转向登录页面
     */
    @RequestMapping(value = "/login")
    public String login(){
        return "/login";
    }

    //检测用户名密码是否匹配，
    @RequestMapping(value = "/toLogin")
    @ResponseBody
    public Object toLogin(String username, String password, HttpSession httpSession) throws ParseException {
        if(username==null||password==null){
            return ResultMapUtil.getHashMapLogin("用户名密码不能为空","2");
        }
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try{
            subject.login(token);
        }catch (UnknownAccountException e){
            return ResultMapUtil.getHashMapLogin("用户名不存在","2");
        }catch (IncorrectCredentialsException e){
            return ResultMapUtil.getHashMapLogin("密码错误","2");
        }

        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        user=userService.queryUserByUsername(user);
        httpSession.setAttribute("user",user);

        String message=username+":登录系统成功";
        logService.LogInfo(LogUtil.getLogInfo(message));

        return ResultMapUtil.getHashMapLogin("验证成功","1");
    }
    @RequestMapping(value = "/register")
    @ResponseBody
    public Object register(String username,String password){
        if(username==null||password==null){
            return ResultMapUtil.getHashMapLogin("用户名密码不能为空","2");
        }
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        try{
            userService.register(user);
        }catch (Exception e){
            return ResultMapUtil.getHashMapLogin("注册失败","2");
        }
        return ResultMapUtil.getHashMapLogin("注册成功","1");
    }

    /**
     * 转向后台管理首页
     */
    @RequestMapping(value = "/index")
    public String index(){
        return "/index";
    }

    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/login";
    }

}
